package by.pavlyukevich.paymentsystem.panel.user;

import java.util.Arrays;

import javax.inject.Inject;

import org.apache.wicket.ajax.AjaxRequestTarget;
import org.apache.wicket.ajax.markup.html.form.AjaxSubmitLink;
import org.apache.wicket.bean.validation.PropertyValidator;
import org.apache.wicket.event.Broadcast;
import org.apache.wicket.markup.html.form.DropDownChoice;
import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.markup.html.form.IChoiceRenderer;
import org.apache.wicket.markup.html.form.TextField;
import org.apache.wicket.markup.html.panel.Panel;
import org.apache.wicket.model.CompoundPropertyModel;
import org.apache.wicket.model.PropertyModel;

import by.pavlyukevich.paymentsystem.add.ErrorEvent;
import by.pavlyukevich.paymentsystem.add.PanelUpdateEvent;
import by.pavlyukevich.paymentsystem.datamodel.user.Contractor;
import by.pavlyukevich.paymentsystem.datamodel.user.Individual;
import by.pavlyukevich.paymentsystem.datamodel.user.UserAccount;
import by.pavlyukevich.paymentsystem.datamodel.user.UserType;
import by.pavlyukevich.paymentsystem.services.user.ContractorService;
import by.pavlyukevich.paymentsystem.services.user.IndividualService;
import by.pavlyukevich.paymentsystem.services.user.UserAccountService;

public class UserAccountEditPanel extends Panel {

	@Inject
	private UserAccountService userAccountService;
	@Inject
	private IndividualService individualService;
	@Inject
	private ContractorService contractorService;

	public UserAccountEditPanel(String id, UserAccount userAccount, Panel respPanel) {
		super(id);
		setOutputMarkupId(true);

		Form<UserAccount> form = new Form<UserAccount>("form",
				new CompoundPropertyModel<UserAccount>(userAccount));

		add(form);

		form.add(new TextField<String>("login").add(new PropertyValidator<String>()));
		form.add(new TextField<String>("password").add(new PropertyValidator<String>()));

		form.add(new DropDownChoice<UserType>("userType", new PropertyModel<UserType>(userAccount,
				"userType"), Arrays.asList(UserType.values()))
				.add(new PropertyValidator<UserType>()));

		form.add(new DropDownChoice<Individual>("individual", new PropertyModel<Individual>(
				userAccount, "individual"), individualService.getList(),
				new IChoiceRenderer<Individual>() {
					@Override
					public Object getDisplayValue(Individual individual) {
						return individual.getFamily() + " " + individual.getName();
					}

					@Override
					public String getIdValue(Individual object, int index) {
						return Integer.toString(index);
					}
				}).add(new PropertyValidator<Individual>()));

		form.add(new DropDownChoice<Contractor>("contractor", new PropertyModel<Contractor>(
				userAccount, "contractor"), contractorService.getList(),
				new IChoiceRenderer<Contractor>() {
					@Override
					public Object getDisplayValue(Contractor contractor) {
						return contractor.getName();
					}

					@Override
					public String getIdValue(Contractor object, int index) {
						return Integer.toString(index);
					}
				}).add(new PropertyValidator<Contractor>()));

		form.add(new AjaxSubmitLink("save") {
			@Override
			protected void onSubmit(AjaxRequestTarget target, Form<?> form) {
				userAccountService.saveOrUpdate(userAccount);
				send(getPage(), Broadcast.BREADTH, new PanelUpdateEvent(target, respPanel));
			}

			@Override
			protected void onError(AjaxRequestTarget target, Form<?> form) {
				send(getPage(), Broadcast.BREADTH, new ErrorEvent(target));
			}

		});

	}
}
